package com.iss.dao.impl;

import com.iss.bean.Consumer;
import com.iss.dao.UserDao;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserDaoImpl implements UserDao {
    @Override
    public Consumer getUsersByUserIdAndPassword(String userId, String password) throws SQLException {
        DataSource ds = new ComboPooledDataSource(); //打开连接池
        Connection conn = ds.getConnection(); //获取连接对象
        Statement statement = conn.createStatement();  //执行sql
        ResultSet rs = statement.executeQuery("select * from users where user_id='" + userId + "' and password='" + password + "'");
        Consumer consumer = new Consumer();
        while (rs.next()) {
            consumer.setUserId(rs.getString(1));
            consumer.setRealName(rs.getString(2));
            consumer.setPassword(rs.getString(3));
            consumer.setSex((rs.getInt(3)==1)?true:false);
            consumer.setAge(rs.getInt(5));
        }
        return consumer;

    }
}
